<?php
FLEA::loadClass('Controller_Base');

class Controller_Firm extends Controller_Base
{
	/**
	 * 数据模块
	 *
	 * @var object
	 */
	var	$_modelFirm;
	/**
	 * firm 模块，
	 *
	 */
	function	Controller_Firm()
	{
		$this->module	=	'Firm';
		$this->_modelFirm	=&	FLEA::getSingleton('Model_Firm');
	}
	
	function actionFirmsList()
	{
		$this->function	=	"FirmsList";
		$firms	=	$this->_modelFirm->findAllFirms();
		$this->viewdata['firms']	=	$firms;
		$this->show();
	}
	/**
	 * 显示welcome 接口
	 *
	 */
	function	actionEditFirm()
	{
		$role	=	$this->checkLogin();
		if($role == 3)
		{
			$firm['FirmID']		=	$this->viewdata['user']['firm']['FirmID'];
			$firm['Name']		=	$_POST['Name'];
			$firm['Address']	=	$_POST['Address'];
			$firm['City']		=	$_POST['City'];
			$firm['State']		=	$_POST['State'];
			$firm['Country']	=	$_POST['Country'];
			$firm['PostalCode']	=	$_POST['PostalCode'];
			
			$result	=	$this->_modelFirm->save($firm);
		}
		else
		{
			$result	=	-2;
		}
		
		$this->viewdata['result']	=	$result;
		$this->viewdata['firm']		=	$this->_modelFirm->findFirm($this->viewdata['user']['firm']['FirmID']);
		$this->function				=	"ShowFirm";
		$this->show();
	}
	/**
	 * add new client
	 *
	 */
	function	actionAddClient()
	{	
		$role	=	$this->checkLogin();
		$_modelUser		=&	FLEA::getSingleton('Model_User');
		$result			=	$_modelUser->clientRegister($_POST['Business'],$_POST['Email'],$_POST['Email'],htmlspecialchars($_POST['Message']),$_POST['FirstName'],$_POST['LastName']);

		$this->showResult($result);
	}
	/**
	 * clients list
	 *
	 */
	function	actionClientsList()
	{
		$this->viewdata['title']	=	'edit firm information';
		$this->viewdata['h2']		=	'Edit your firm information';
		$this->function		=	'ClientsList';
		$this->show();
	}
	/**
	 * 显示 addpractitioner 接口
	 *
	 */
	function	actionAddPractitioner()
	{
		$isLogin	=	$this->checkLogin();
		
		if($isLogin == 3)
		{
			$practitioner['FirmID']		=	$this->viewdata['user']['firm']['FirmID'];
			$practitioner['BAttorney']	=	$_POST['BAttorney'];
			$practitioner['PatentBar']	=	$_POST['PatentBar'];
			$practitioner['Editor']	=	$_POST['Editor'];
			$practitioner['Draftsman']	=	$_POST['Draftsman'];
			
			$user['FirstName']	=	$_POST['FirstName'];
			$user['LastName']	=	$_POST['LastName'];
			$user['Email']		=	$_POST['Email'];
			
			$message			=	$_POST['Message'];
			
			$result	=	$this->_modelFirm->addPractitioner($user,$practitioner,$message);
		}
		else
		{
			$result	=	-2;
		}
		
		$this->viewdata['result']	=	$result;
		$this->function				=	'PractitionersList';
		$_modelPractitioner			=&	FLEA::getSingleton('Model_Practitioner');
		
		$this->viewdata['practitioners']=	$_modelPractitioner->findAllPractitioners($this->viewdata['user']['firm']['FirmID']);
		$this->show();
	}
	
	function	actionDeletePractitioner()
	{
		$isLogin	=	$this->checkLogin();
		if($isLogin == 3)
		{
			$userID		=	$_POST['UserID'];
			$_modelUser	=&	FLEA::getSingleton('Model_User');
			$result		=	$_modelUser->removeByPkv($userID);
		}
		else
		{
			$result	=	-2;
		}
		
		$this->viewdata['result']	=	$result;
		$this->function				=	'PractitionersList';
		$_modelPractitioner			=&	FLEA::getSingleton('Model_Practitioner');
		if($isLogin == 3)
		{
			$this->viewdata['practitioners']=	$_modelPractitioner->findAllPractitioners($this->viewdata['user']['firm']['FirmID']);
		}
		$this->show();
	}
	
	function	actionPractitionersList()
	{
		$_modelPractitioner	=&	FLEA::getSingleton('Model_Practitioner');
		$isLogin	=	$this->checkLogin();
		if($isLogin == 3)
		{
			$practitioners	=	$_modelPractitioner->findAllPractitioners($this->viewdata['user']['firm']['FirmID']);
			$this->function	=	"PractitionersList";
			$this->viewdata['practitioners']	=	$practitioners;
			$this->show();
		}
		else
		{
			$this->showResult(-2);
		}
	}
	
	function	actionViewPractitioner()
	{
		$this->viewdata['title']	=	'practitioner information';
		$this->viewdata['h2']		=	'Practitioner information';
		$this->function		=	'ViewPractitioner';
		
		$_modelPractitioner	=&	FLEA::getSingleton('Model_Practitioner');
		$practitioner		=	$_modelPractitioner->findPractitioner($_GET['PractitionerID']);
		$this->viewdata['practitioner']	=	$practitioner;
		$this->show();
	}
	
	function	actionUpdatePractitioner()
	{
		$role	=	$this->checkLogin();
		$_modelPractitioner	=&	FLEA::getSingleton('Model_Practitioner');
		if($role == 3)
		{
			
			
			$practitioner['PractitionerID']	=	$_POST['PractitionerID'];
			$practitioner['BAttorney']		=	$_POST['BAttorney'];
			$practitioner['BAgent']			=	$_POST['BAgent'];
			$practitioner['PatentBar']		=	$_POST['PatentBar'];
			$practitioner['Editor']			=	$_POST['Editor'];
			$practitioner['Draftsman']		=	$_POST['Draftsman'];
			
			$result	=	$_modelPractitioner->save($practitioner);
		}
		else
		{
			$result	=	-2;
		}
		
		$this->viewdata['result']	=	$result;
		$this->function				=	'ShowPractitioner';
		
		if($role == 3)
		{
			$this->viewdata['practitioner']=	$_modelPractitioner->findPractitioner($_POST['PractitionerID']);
		}
		$this->show();
	}
	
	function	actionUpdateFirm()
	{
		$isLogin	=	$this->checkLogin();
		if($isLogin == 5)
		{
			$firm['FirmID']		=	$_POST['FirmID'];
			
			
			$firmTem		=	$this->_modelFirm->findFirm($firm['FirmID']);

			$firm['BAuction']	=	$firm['BAuction'] == 1? 0 :1;

			$result	=	$this->_modelFirm->save($firm);
			
		}
		else
		{
			$result	=	-2;
		}
		
		$this->showResult($result);
	}
}
?>